package com.epro.dx.adapter;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;
import java.util.Locale;
import java.util.TimeZone;

import com.epro.dx.R;
import com.epro.dx.listenner.MyAsyncTaskListenner;
import com.epro.dx.task.DeleteNoticesAsyncTask;
import com.epro.dx.util.AsyncTaskUtil;
import com.epro.dx.util.ImageLoaderUtils;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.weipu.common.facade.MessageServiceProvider;
import com.weipu.common.facade.model.MessageModel;

import android.annotation.SuppressLint;
import android.content.Context;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnLongClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

/************************
 * @author Tony
 * @Date:2016-4-7下午3:08:03
 * @描述:消息列表数据适配器
 ************************/
public class MessageListAdapter extends BaseAdapter {

	private Context mContext;
	private boolean isPromotion;
	private List<MessageModel> datas;
	private MessageClickListener messageClickListener;
	private DisplayImageOptions imageLoaderOptions;
	private ImageLoader imageLoader;

	/**
	 * @param mContext 上下文
	 * @param isPromotion 是否是活动列表
	 * @param datas 数据模型列表
	 */
	public MessageListAdapter(Context mContext,boolean isPromotion,List<MessageModel> datas,MessageClickListener messageClickListener){
		this.mContext = mContext;
		this.isPromotion = isPromotion;
		this.datas = datas;
		this.messageClickListener = messageClickListener;
		imageLoaderOptions = ImageLoaderUtils.getImageLoaderOptions(R.drawable.default1080_875, R.drawable.default1080_875);
		imageLoader = ImageLoader.getInstance();
	}
	@Override
	public int getCount() {
		//根据数据列表确定数量
		if(null!=datas){
			return datas.size();
		}
		return 0;
	}

	@Override
	public Object getItem(int position) {
		if(null!=datas){
			return datas.get(position);
		}
		return null;
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		HolderView holder;
		if(null == convertView){
			holder = new HolderView();
			convertView = View.inflate(mContext, R.layout.item_message_system_list, null);
			holder.img = (ImageView) convertView.findViewById(R.id.item_message_event_list_img);
			holder.title = (TextView) convertView.findViewById(R.id.item_message_event_list_title);
			holder.smallTitle = (TextView) convertView.findViewById(R.id.item_message_event_list_smalltitle);
			holder.time = (TextView) convertView.findViewById(R.id.item_message_event_list_time);
			holder.viewMore = (RelativeLayout) convertView.findViewById(R.id.item_messagelist_viewmore);
			holder.icon = (ImageView) convertView.findViewById(R.id.item_message_event_list_icon);
			convertView.setTag(holder);
		}else{
			holder = (HolderView) convertView.getTag();
		}
		//数据处理
		MessageModel messageModel = datas.get(position);
		if(isPromotion){
			holder.viewMore.setVisibility(View.GONE);
		}else{
			if(messageModel.getLinkType()!=MessageModel.LINKTYPE_CONTENTTEXT){
				holder.viewMore.setVisibility(View.VISIBLE);
			}else{
				holder.viewMore.setVisibility(View.GONE);
			}
		}
		//图片与图标显示设置
		String imgUrl = messageModel.getImg();
		if(!TextUtils.isEmpty(imgUrl)){
			holder.icon.setVisibility(View.GONE);
			holder.img.setVisibility(View.VISIBLE);
			//加载图片
			imageLoader.displayImage(imgUrl, holder.img, imageLoaderOptions);
		}else{
			holder.icon.setVisibility(View.VISIBLE);
			holder.img.setVisibility(View.GONE);
		}
		//标题
		String title = messageModel.getTitle();
		if(!TextUtils.isEmpty(title)){
			holder.title.setVisibility(View.VISIBLE);
			holder.title.setText(title);
		}else{
			holder.title.setVisibility(View.GONE);
		}
		//内容
		String content = messageModel.getContent();
		if(!TextUtils.isEmpty(content)){
			holder.smallTitle.setVisibility(View.VISIBLE);
			holder.smallTitle.setText(content);
		}else{
			holder.smallTitle.setVisibility(View.GONE);
		}
		//时间设置
		String createAt = messageModel.getCreateAt();
		if(!TextUtils.isEmpty(createAt)){
			holder.time.setText(formatTimeToLocal(createAt));
		}
		//事件
		convertView.setOnLongClickListener(new OnLongClickListener() {
			
			@Override
			public boolean onLongClick(View v) {
				//长按删除消息
				if(null!=messageClickListener){
					messageClickListener.onMessageLongClickListener(v,position);
				}
				return false;
			}
		});
		convertView.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				//点击跳转
				if(null!=messageClickListener){
					messageClickListener.onMessageClickListener(v,position);
				}
			}
		});
		return convertView;
	}

	/**
	 * @创建：Tony 2016-5-6下午4:53:16
	 * @描述：根据本地时区格式化显示发信时间
	 * @param createAt
	 * @return
	 */
	private CharSequence formatTimeToLocal(String createAt) {
		SimpleDateFormat localDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		TimeZone pst = TimeZone.getTimeZone("Etc/GMT+0");
		try {
			localDateFormat.setTimeZone(pst);
			Date oldDate = localDateFormat.parse(createAt);
			localDateFormat.setTimeZone(TimeZone.getDefault());
			String oldDateText = localDateFormat.format(oldDate);
			Date newDate = new Date(System.currentTimeMillis());
			String newDateText = localDateFormat.format(newDate);
			String[] splitOldDate = oldDateText.split(" ");
			String[] splitNewDate = newDateText.split(" ");
			String[] timeSplit = splitOldDate[1].split(":");
			String time = timeSplit[0]+":"+timeSplit[1];
			if((splitNewDate[0]).equals(splitOldDate[0])){
				//今天的消息，显示时间
				return time;
			}else{
				Calendar calendar = new GregorianCalendar();
				calendar.setTime(newDate);
				calendar.add(Calendar.DATE,-1);//把日期往前减一天.整数往后推,负数往前移动
				String yesterday = localDateFormat.format(calendar.getTime());//昨天的日期
				yesterday = yesterday.split(" ")[0];
				if(yesterday.equals(splitOldDate[0])){
					return mContext.getString(R.string.yesterday)+" "+time;
				}else{
					return splitOldDate[0]+" "+time;
				}
			}
		} catch (ParseException e) {
			return null;
		}
	}

	class HolderView{
		ImageView img;
		ImageView icon;
		TextView title;
		TextView smallTitle;
		TextView time;
		//系统消息下方的查看更多
		RelativeLayout viewMore;
	}
	
	public interface MessageClickListener{
		public void onMessageClickListener(View v,int position);
		public void onMessageLongClickListener(View v,int position);
	}
}
